<template>
  <div class="type-nav">
    <div class="container" @mouseleave="leaveShow">
      <h2 class="all" @mouseenter="enterShow">全部文具分类</h2>
      
      <transition name="sort">
      <div class="sort" v-show="show" >
        <div class="all-sort-list2" @click="goSearch">
          <div class="item" v-for="(main,index) in classList" :key="main.mainClassId" :class="{cur:currentIndex==index}">
            <h3 @mouseenter="changeIndex(index)" @mouseleave="leaveIndex">
              <a :data-className="main.mainClassName" :data-mainClassId="main.mainClassId">{{ main.mainClassName }}</a>
            </h3>
            <div class="item-list clearfix" >
              <div
                class="subitem"
                v-for="other in main.otherClasses"
                :key="other.otherClassId"
              >
                <dl class="fore">
                  <dt>
                    <a :data-className="other.otherClassName" :data-otherClassId="other.otherClassId" >{{ other.otherClassName }}</a>
                  </dt>
                </dl>
              </div>
            </div>
          </div>
        </div>
      </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import throttle from 'lodash/throttle';
export default {
  name: "TypeNav",
  //组件挂载完毕，可以向服务器发送请求
  data() {
    return {
      //存储鼠标移上哪个一级分类
      currentIndex:-1,
      show:true,
    };
  },
  computed: {
    ...mapState({
      classList: (state) => state.home.classList,
    }),
  },
  mounted() {
    if(this.$route.path!='/home'){
      this.show=false;
    }
  },

  methods: {
    //鼠标进入修改响应式数据currentIndex
    changeIndex:throttle(function(index){
      this.currentIndex=index;
    },50),
    leaveIndex(){
      this.currentIndex=-1;
    },
    //编程式导航加上事件的委派以解决卡顿问题
    goSearch(event){
      let element =event.target;
      //如果标签上有categoryname一定是a标签
      //categoryid区分哪级分类
      let {classname,mainclassid,otherclassid} =element.dataset;
      if(classname){
        //整理路由跳转的参数
        let location ={name:'search',path:'/search'};
        let query = {className:classname};
        if(mainclassid){
          query.mainClassId= mainclassid;        
        }else{
          query.otherClassId= otherclassid;    
        }
        //整理完参数
        //判断:如果路由跳转时带有params参数,捎带着传递过去
        if(this.$route.params){
          location.params=this.$route.params;
        }
        location.query =query;
        this.$router.push(location);
      }
    },
    enterShow(){
      this.show = true;
    },
    leaveShow(){
        this.show = false;
    }
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 200px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 180px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }
                &.fore:hover {
                  background: skyblue;
                }
                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
        .cur{
          background: skyblue;
        }
      }
    }
    //过渡动画样式
    .sort-enter{
      height: 0px;
    }
    .sort-enter-to{
      height: 461px;
    }
    .sort-enter-active{
      transition: all .5s linear;
    }
  }
}
</style>